package com.study.mynew.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.study.mynew.R;
import com.study.mynew.bean.User;

import org.litepal.LitePal;


public class SignupActivity extends AppCompatActivity {
    private Button signup_btn;
    private EditText signup_name, signup_password, signup_password_again;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_signup);


        signup_btn = findViewById(R.id.signup_btn);
        signup_name = findViewById(R.id.account);
        signup_password = findViewById(R.id.password);
        signup_password_again = findViewById(R.id.passwordConfirm);

        signup_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String account = signup_name.getText().toString();
                String password = signup_password.getText().toString();
                String passwordAgain = signup_password_again.getText().toString();
                if (account.equals("") || password.equals("") || passwordAgain.equals("")) {
                    Toast.makeText(SignupActivity.this, "请输入账号或密码", Toast.LENGTH_SHORT).show();
                } else {
                    if (password.equals(passwordAgain)) {
                        Boolean check = checkAccount(account);
                        if (!check) {
                            insertData(account, password);
                            Toast.makeText(SignupActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent(SignupActivity.this, LoginActivity.class);
                            startActivity(intent);
                            finish();

                        } else {
                            Toast.makeText(SignupActivity.this, "账号已存在", Toast.LENGTH_SHORT).show();
                        }
                    } else {
                        Toast.makeText(SignupActivity.this, "密码不一致", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
    }

    public void insertData(String account, String password) {
        User user = new User();
        user.setUsername(account);
        user.setPassword(password);
        user.save();

    }

    public Boolean checkAccount(String account) {
        User user = LitePal.where("username = ? ", account).findFirst(User.class);
        return user != null;
    }

//    public Boolean checkPassword(String account, String password) {
//        SQLiteDatabase sqLiteDatabase = databaseHelper.getReadableDatabase();
//        Cursor cursor = sqLiteDatabase.rawQuery("select * from User where name = ? and password = ?", new String[]{account, password});
//        if(cursor.getCount() > 0) {
//            return true;
//        } else {
//            return false;
//        }
//    }
}